﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Reflection;
using eCommerce.db.DataRespository;
using eCommerce.db.SQLDataAccess;

namespace eCommerce.db.DALRespository
{
    public class ImageDal
    {
        #region ImageDal private
        private static void SetInfo(IDataRecord reader, ref ImageEntity o)
        {
            for (int i = 0; i < reader.FieldCount; i++)
            {
                PropertyInfo propertyInfo = DbCommon.GetPropertyInfo(o, reader.GetName(i));
                if (((propertyInfo != null) && (reader[i] != null)) && (reader[i] != DBNull.Value))
                    propertyInfo.SetValue(o, reader[i], null);
                else
                    o[reader.GetName(i)] = reader[i];
            }
        }

        private static void SetInfoList(ref SqlDataReader reader, ref List<ImageEntity> imageList)
        {
            while (reader.Read())
            {
                var o = new ImageEntity();
                SetInfo(reader, ref o);
                imageList.Add(o);
            }
            reader.Dispose();
        }
        #endregion

        public static List<ImageEntity> GetByProductId(int productId)
        {
            DbConnection.SetConnection(DbConnection.ConnectionName.Main);
            var images = new List<ImageEntity>();
            try
            {
                var reader = SqlHelper.ExecuteReader(DbCommon.ConnectionString, DbCommon.DatabaseSchema + "Image_GetByProductId", productId);
                    SetInfoList(ref reader, ref images);
                return images;
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format(DbCommon.DatabaseSchema + "Image_GetByProductId: {0}", ex.Message));
            }
        }

        public static ImageEntity GetById(int id)
        {
            DbConnection.SetConnection(DbConnection.ConnectionName.Main);
            var image = new ImageEntity();
            try
            {
                var reader = SqlHelper.ExecuteReader(DbCommon.ConnectionString, DbCommon.DatabaseSchema + "Image_GetById", id);
                if (reader.Read())
                    SetInfo(reader, ref image);
                return image;
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format(DbCommon.DatabaseSchema + "Image_GetById: {0}", ex.Message));
            }
        }

        public static void Create(ImageEntity o)
        {
            SqlHelper.ExecuteNonQuery(DbCommon.ConnectionString, DbCommon.DatabaseSchema + "Image_Create", o.ProductId, o.Priority, o.ImagePath);
        }

        public static void Delete(int id)
        {            
            SqlHelper.ExecuteNonQuery(DbCommon.ConnectionString, DbCommon.DatabaseSchema + "Image_Delete", id);
        }

        public static int GetImageCount(int productId)
        {
            return SqlHelper.ExecuteNonQuery(DbCommon.ConnectionString, DbCommon.DatabaseSchema + "Image_GetImageCount", productId);
        }        
    }
}
